package com.sgcc.statistics.service.impl;

import java.beans.ConstructorProperties;
import java.io.IOException;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Date;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sgcc.common.core.domain.R;
import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.statistics.domain.*;
import com.sgcc.statistics.mapper.CConsMapper;
import com.sgcc.statistics.service.*;
import com.sgcc.system.api.RemoteDeptService;
import com.sgcc.system.api.domain.SysDept;
import com.sgcc.system.api.model.LoginUser;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.statistics.mapper.OIndexBreakdownMapper;

import static java.util.Comparator.comparing;

/**
 * OIndexBreakdownService业务层处理
 * 
 * @author sgcc
 * @date 2021-04-14
 */
@Service
public class OIndexBreakdownServiceImpl implements IOIndexBreakdownService 
{

    private Logger logger = LoggerFactory.getLogger(OIndexBreakdownServiceImpl.class);
    @Autowired
    private OIndexBreakdownMapper oIndexBreakdownMapper;

    @Autowired
    private RemoteDeptService remoteDeptService;

    @Autowired
    private IOSgappStatService ioSgappStatService;

    @Autowired
    private CConsMapper cConsMapper;

    @Autowired
    private IOConsPercentageService ioConsPercentageService;

    @Autowired
    private IOPowerBindRateService ioPowerBindRateService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询OIndexBreakdown
     * 
     * @param id OIndexBreakdownID
     * @return OIndexBreakdown
     */
    @Override
    public OIndexBreakdown selectOIndexBreakdownById(String id)
    {
        return oIndexBreakdownMapper.selectOIndexBreakdownById(id);
    }

    /**
     * 查询OIndexBreakdown列表
     * 
     * @param oIndexBreakdown OIndexBreakdown
     * @return OIndexBreakdown
     */
    @Override
    public List<OIndexBreakdown> selectOIndexBreakdownList(OIndexBreakdown oIndexBreakdown) throws Exception {
        if(null != oIndexBreakdown && null != oIndexBreakdown.getYear()){
            return oIndexBreakdownMapper.selectOIndexBreakdownListByYearAndUnit(oIndexBreakdown.getYear());
        }
        return oIndexBreakdownMapper.selectOIndexBreakdownList(oIndexBreakdown);
    }

    /**
     * 新增OIndexBreakdown
     * 
     * @param oIndexBreakdownList
     * @return 结果
     */
    @Override
    public int insertOIndexBreakdown(List<OIndexBreakdown> oIndexBreakdownList) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser();
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] json = objectMapper.writeValueAsBytes(oIndexBreakdownList);
        OIndexBreakdown[] oIndexBreakdowns = objectMapper.readValue(json, OIndexBreakdown[].class);
        List<OIndexBreakdown> oatdList = new ArrayList<>();
        Collections.addAll(oatdList, oIndexBreakdowns);
        for (OIndexBreakdown oIndexBreakdown : oatdList){
            // 先查询数据库是否有当前数据，有做修改操作，没有做保存操作
            OIndexBreakdown oIndexBreakdown1 = oIndexBreakdownMapper.selectOIndexBreakdownByYearAndUnit(oIndexBreakdown.getUnit(),oIndexBreakdown.getYear(),oIndexBreakdown.getPeriod());
            if (null != oIndexBreakdown1){
                oIndexBreakdown1.setUpdateTime(DateUtils.getNowDate());
                oIndexBreakdown1.setUpdateBy(loginUser.getUsername());
                oIndexBreakdown1.setStatus(oIndexBreakdown.getStatus());
                oIndexBreakdown1.settRegNum(oIndexBreakdown.gettRegNum());
                oIndexBreakdown1.setnRegNum(oIndexBreakdown.getnRegNum());
                oIndexBreakdown1.settBindNum(oIndexBreakdown.gettBindNum());
                oIndexBreakdown1.setnBindNum(oIndexBreakdown.getnBindNum());
                oIndexBreakdown1.settFirmBindNum(oIndexBreakdown.gettFirmBindNum());
                oIndexBreakdown1.setnFirmBindNum(oIndexBreakdown.getnFirmBindNum());
                oIndexBreakdownMapper.updateOIndexBreakdown(oIndexBreakdown1);
            }else {
                String id= UUID.randomUUID().toString().replaceAll("-", "");
                oIndexBreakdown.setId(id);
                oIndexBreakdown.setCreateTime(DateUtils.getNowDate());
                oIndexBreakdown.setCreateBy(loginUser.getUsername());
                oIndexBreakdownMapper.insertOIndexBreakdown(oIndexBreakdown);
            }
        }
        return 1;
    }

    /**
     * 修改OIndexBreakdown
     * 
     * @param OIndexBreakdownList
     * @return 结果
     */
    @Override
    public int updateOIndexBreakdown(List<OIndexBreakdown> OIndexBreakdownList) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser();
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] json = objectMapper.writeValueAsBytes(OIndexBreakdownList);
        OIndexBreakdown[] oIndexBreakdowns = objectMapper.readValue(json, OIndexBreakdown[].class);
        List<OIndexBreakdown> oatdList = new ArrayList<>();
        Collections.addAll(oatdList, oIndexBreakdowns);
        for (OIndexBreakdown oIndexBreakdown : oatdList){
            oIndexBreakdown.setUpdateTime(DateUtils.getNowDate());
            oIndexBreakdown.setUpdateBy(loginUser.getUsername());
            return oIndexBreakdownMapper.updateOIndexBreakdown(oIndexBreakdown);
        }
        return 1;
    }

    /**
     * 批量删除OIndexBreakdown
     * 
     * @param ids 需要删除的OIndexBreakdownID
     * @return 结果
     */
    @Override
    public int deleteOIndexBreakdownByIds(String[] ids)
    {
        return oIndexBreakdownMapper.deleteOIndexBreakdownByIds(ids);
    }

    /**
     * 删除OIndexBreakdown信息
     * 
     * @param id OIndexBreakdownID
     * @return 结果
     */
    @Override
    public int deleteOIndexBreakdownById(String id)
    {
        return oIndexBreakdownMapper.deleteOIndexBreakdownById(id);
    }

    @Override
    public List<OIndexBreakdown> provinceIndexBrekdown(OIndexBreakdown oIndexBreakdown) throws Exception {
        if (null != oIndexBreakdown){
            // 查询出省下面所有的地市
            R r = remoteDeptService.getUnit("35101");
            if (null != r && r.getCode() == 200){
                // List<SysDept> sysDeptList = (List<SysDept>) r.getData();
                List<SysDept> sysDeptList = getSysDepts(r);

                List<OIndexBreakdown> oIndexBreakdownList = new ArrayList<OIndexBreakdown>();
                // 全省的值手动添加
                OIndexBreakdown allOindexb  = new OIndexBreakdown();
                allOindexb.setUnit("35101");
                allOindexb.setYear(oIndexBreakdown.getYear());
                allOindexb.settRegNum(oIndexBreakdown.getProtRegNum());
                // 查询上一年全省的累计注册数，累计绑定数和累计企业用户绑定数
                List<String> OrgNoList = new ArrayList<>();
                for (SysDept sysDept : sysDeptList){
                    OrgNoList.add(sysDept.getDeptId());
                }
                // 获取去年的最后一天的时间
                int lastYear = Integer.valueOf(oIndexBreakdown.getYear()) - 1;
                String lastYearstrTime = lastYear + "-12-31 00:00:00";
                String lastYearendTime = lastYear + "-12-31 23:59:59";
                DecimalFormat    df   = new DecimalFormat("######0.0000");
                OSgappStat alloSgappStat = ioSgappStatService.getOsgappStatByUnitList(OrgNoList,lastYearstrTime,lastYearendTime);
                if (null != alloSgappStat){
                    if (null != alloSgappStat.getSgappTRegNum()){
                        double nRegNum = oIndexBreakdown.getProtRegNum() - alloSgappStat.getSgappTRegNum();
                        String format = df.format(nRegNum);
                        allOindexb.setnRegNum(Double.valueOf(format));
                    }
                    if (null != alloSgappStat.getSgappTBindNum()) {
                        double nBindNum = oIndexBreakdown.getProtBindNum() - alloSgappStat.getSgappTBindNum();
                        String format = df.format(nBindNum);
                        allOindexb.setnBindNum(Double.valueOf(format));
                    }
                    if(null != alloSgappStat.getFirmBindNum()){
                        double induconsBind = oIndexBreakdown.getProtFirmBindNum() - alloSgappStat.getFirmBindNum();
                        String format = df.format(induconsBind);
                        allOindexb.setnFirmBindNum(Double.valueOf(format));
                    }
                }
                // OPowerBindRate oPowerBindRate1 = ioPowerBindRateService.selectOPowerBindRateByUnitList(OrgNoList, lastYearstrTime, lastYearendTime);


                allOindexb.settBindNum(oIndexBreakdown.getProtBindNum());
                allOindexb.settFirmBindNum(oIndexBreakdown.getProtFirmBindNum());
                oIndexBreakdownList.add(allOindexb);
                for (SysDept sysDept : sysDeptList){
                    String orgNo = sysDept.getDeptId();
                    // 查询地市对应的户数占比
                    OIndexBreakdown oIndexb  = new OIndexBreakdown();
                    // 根据年最后一天获取当前单位的占比
                    OConsPercentage oConsPercentage = ioConsPercentageService.selectOConsPercentageByunitAndTime(orgNo,lastYearstrTime,lastYearendTime);
                    if (null != oConsPercentage){
                        // 计算累计注册量
                        double tRegNum = oIndexBreakdown.getProtRegNum() * oConsPercentage.getPercentage();
                        String format = df.format(tRegNum);
                        oIndexb.settRegNum(Double.valueOf(format));
                        // 累计绑定数
                        double tBindNum = oIndexBreakdown.getProtBindNum() * oConsPercentage.getPercentage();
                        String format2 = df.format(tBindNum);
                        oIndexb.settBindNum(Double.valueOf(format2));
                        // 新增注册数
                        // 查询上一年地市的累计注册数
                        OSgappStat oSgappStat = ioSgappStatService.getOsgappStatByUnit(orgNo,lastYearstrTime, lastYearendTime);
                        if(null != oSgappStat){
                            double nregNum = tRegNum - oSgappStat.getSgappTRegNum();
                            String format1 = df.format(nregNum);
                            oIndexb.setnRegNum(Double.valueOf(format1));
                            // 新增绑定数
                            double nBindNum = tBindNum - oSgappStat.getSgappTBindNum();
                            String format3 = df.format(nBindNum);
                            oIndexb.setnBindNum(Double.valueOf(format3));
                        }
                        //  累计企业用户绑定数
                        double tFirmBindNum = oIndexBreakdown.getProtFirmBindNum() * oConsPercentage.getQyPercentage();
                        String format1 = df.format(tFirmBindNum);
                        oIndexb.settFirmBindNum(Double.valueOf(format1));
                        // 新增企业用户绑定数
                        // 查询非用户绑定数
                        OPowerBindRate oPowerBindRate = ioPowerBindRateService.selectOPowerBindRateByUnit(orgNo,lastYearstrTime,lastYearendTime);
                        if (null != oPowerBindRate){
                            double nfirmBindNum = 0.0;
                            if (null == oSgappStat){
                                nfirmBindNum = tFirmBindNum - oPowerBindRate.getNoresideConsBind();
                            }else {
                                nfirmBindNum = tFirmBindNum - (oSgappStat.getHigBindNum() + oPowerBindRate.getNoresideConsBind());
                            }
                            String format3 = df.format(nfirmBindNum);
                            oIndexb.setnFirmBindNum(Double.valueOf(format3));
                        }
                    }
                    oIndexb.setUnit(orgNo);
                    oIndexb.setYear(oIndexBreakdown.getYear());
                    /*// 获取年
                    oIndexb.setYear(oIndexb.getYear());
                    oIndexb.setCreateTime(new Date());
                    oIndexb.setCreateTime(DateUtils.getNowDate());
                    // 保存9地市的数据
                    oIndexBreakdownMapper.insertOIndexBreakdown(oIndexb);*/
                    oIndexBreakdownList.add(oIndexb);
                }

                return oIndexBreakdownList;
            }
        }
        return new ArrayList<OIndexBreakdown>();
    }

    private List<SysDept> getSysDepts(R r) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] json = objectMapper.writeValueAsBytes(r.getData());
        SysDept[] sysDepts = objectMapper.readValue(json, SysDept[].class);
        List<SysDept> sysDeptList = new ArrayList<>();
        Collections.addAll(sysDeptList, sysDepts);
        return sysDeptList;
    }

    @Override
    public List<OIndexBreakdown> cityIndexBrekdown(OIndexBreakdown oIndexBreakdown) throws Exception {
        if (null != oIndexBreakdown){
            //  查询当前地市下面的所有区县
            R r = remoteDeptService.getUnit(oIndexBreakdown.getUnit());
            List<SysDept> sysDeptList = new ArrayList<>();
            if (null != r && 200 == r.getCode()){
                List litst = (List) r.getData();
                if (litst.size() > 0){
                    sysDeptList = getSysDepts(r);
                }else {
                    SysDept sysDept = new SysDept();
                    sysDept.setDeptId(oIndexBreakdown.getUnit());
                    sysDeptList.add(sysDept);
                }
            }else{
                SysDept sysDept = new SysDept();
                sysDept.setDeptId(oIndexBreakdown.getUnit());
                sysDeptList.add(sysDept);
            }
            List<OIndexBreakdown> oIndexBreakdownList = new ArrayList<OIndexBreakdown>();
            // List<SysDept> sysDeptList = (List<SysDept>) r.getData();
            // List<SysDept> sysDeptList = getSysDepts(r);
            List<String> selectTimeList = oIndexBreakdown.getSelectTime();
            //判断当前是否是1月 如果是直接分解，不是则要减去已完成的注册数、累计数
            //String nowTime = DateUtils.convertToString(new Date(), "MM-dd");
            // 查询出去年底的累计注册数，累计绑定数和企业累计绑定数
            String lastYear = DateUtils.getLastYear();
            String sTime = lastYear + "-12-31 00:00:00";
            String eTime = lastYear + "-12-31 23:59:59";
            OSgappStat lastOSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),sTime,eTime);
            // 查询去年底的非居民绑定数
            OPowerBindRate oPowerBindRateCity = ioPowerBindRateService.selectOPowerBindRateByUnit(oIndexBreakdown.getUnit(),sTime,eTime);
            int nowMonth = DateUtils.getNowMonth();
            // OIndexBreakdown indexByUnitAndYearAndMonth = oIndexBreakdownMapper.getIndexByUnitAndYearAndMonth(oIndexBreakdown.getUnit(), oIndexBreakdown.getYear(), "1");
            if (1 == nowMonth){
                selectTimeList.clear();
                for (int i=1; i<=12; i++){
                    selectTimeList.add(i+"");
                }
                if (null != lastOSgappStat){
                    // 当前的累计注册数 - 去年的累计注册数 = 本年新增注册数
                    double tregNum = oIndexBreakdown.gettRegNum() - lastOSgappStat.getSgappTRegNum();
                    int tregNum1 = (int) tregNum;
                    oIndexBreakdown.setnRegNum(Double.valueOf(tregNum1));
                    // 当前的累计绑定数 - 去年的累计绑定数 = 本年新增绑定数
                    double tBindNum = oIndexBreakdown.gettBindNum() - lastOSgappStat.getSgappTBindNum();
                    int tBindNum1 = (int) tBindNum;
                    oIndexBreakdown.setnBindNum(Double.valueOf(tBindNum1));
                }
                // sTime = DateUtils.getLastYear() + "-01-01 00:00:00";

                // 当前的企业累计绑定数 - 去年底的企业累计绑定数 = 本年新增的企业累计绑定数
                if (null != oPowerBindRateCity && null != lastOSgappStat){
                    double nFirmBindNum = oIndexBreakdown.gettFirmBindNum() - (lastOSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind());
                    int nFirmBindNum1 = (int) nFirmBindNum;
                    oIndexBreakdown.setnFirmBindNum(Double.valueOf(nFirmBindNum1));
                }else if(null != lastOSgappStat){
                    double nFirmBindNum = oIndexBreakdown.gettFirmBindNum() - lastOSgappStat.getHigBindNum();
                    int nFirmBindNum1 = (int) nFirmBindNum;
                    oIndexBreakdown.setnFirmBindNum(Double.valueOf(nFirmBindNum1));
                }else {
                    double nFirmBindNum = oIndexBreakdown.gettFirmBindNum() - oPowerBindRateCity.getNoresideConsBind();
                    int nFirmBindNum1 = (int) nFirmBindNum;
                    oIndexBreakdown.setnFirmBindNum(Double.valueOf(nFirmBindNum1));
                }


                double nregNum = oIndexBreakdown.getnRegNum() / selectTimeList.size();
                int format6 = (int) nregNum;
                double v = oIndexBreakdown.getnRegNum() % selectTimeList.size();
                int v1 = (int) v;
                double tregNum = oIndexBreakdown.gettRegNum() / selectTimeList.size();
                int format7 = (int) tregNum;
                double v2 = oIndexBreakdown.gettRegNum() % selectTimeList.size();
                int v3 = (int) v2;
                double tbindNum = oIndexBreakdown.gettBindNum() / selectTimeList.size();
                int format8 = (int) tbindNum;
                double v4 = oIndexBreakdown.gettBindNum() % selectTimeList.size();
                int v5 = (int) v4;
                double nbindNum = oIndexBreakdown.getnBindNum() / selectTimeList.size();
                int format9 = (int) nbindNum;
                double v6 = oIndexBreakdown.getnBindNum() % selectTimeList.size();
                int v7 = (int) v6;

                double tfirmBindNum = oIndexBreakdown.gettFirmBindNum() / selectTimeList.size();
                int format10 = (int) tfirmBindNum;
                double v8 = oIndexBreakdown.gettFirmBindNum() % selectTimeList.size();
                int v9 = (int) v8;
                double nfirmBindNum = oIndexBreakdown.getnFirmBindNum() / selectTimeList.size();
                int format11 = (int) nfirmBindNum;
                double v10 = oIndexBreakdown.getnFirmBindNum() % selectTimeList.size();
                int v11 = (int) v10;
                int count = 0;
                for(String breakTime : selectTimeList){
                    count ++;
                    OIndexBreakdown oIndexb  = new OIndexBreakdown();
                    oIndexb.setUnit(oIndexBreakdown.getUnit());
                    oIndexb.setYear(oIndexBreakdown.getYear());
                    oIndexb.setPeriod(breakTime);
                    if (count == selectTimeList.size()){
                        int i = format6 + v1;
                        oIndexb.setnRegNum(Double.valueOf(i));
                        int i1 = format7 + v3;
                        int i2 = format8 + v5;
                        int i4 = format10 + v9;
                        if (null != lastOSgappStat){
                            long v12 = lastOSgappStat.getSgappTRegNum() + format7 * count + v3;
                            oIndexb.settRegNum(Double.valueOf(v12));
                            long l = lastOSgappStat.getSgappTBindNum() + format8 * count + v5;
                            oIndexb.settBindNum(Double.valueOf(l));
                            if (null != oPowerBindRateCity){
                                long l1 = (lastOSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind()) + format10 * count + v9;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }else {
                                long l1 = lastOSgappStat.getHigBindNum() + format10 * count + v9;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }
                        }else {
                            oIndexb.settRegNum(Double.valueOf(i1));
                            oIndexb.settBindNum(Double.valueOf(i2));
                            oIndexb.settFirmBindNum(Double.valueOf(i4));
                        }
                        int i3 = format9 + v7;
                        oIndexb.setnBindNum(Double.valueOf(i3));
                        int i5 = format11 + v11;
                        oIndexb.setnFirmBindNum(Double.valueOf(i5));
                    }else {
                        oIndexb.setnRegNum(Double.valueOf(format6));
//                        oIndexb.settRegNum(Double.valueOf(format7));
//                        oIndexb.settBindNum(Double.valueOf(format8));
                        oIndexb.setnBindNum(Double.valueOf(format9));
//                        oIndexb.settFirmBindNum(Double.valueOf(format10));
                        oIndexb.setnFirmBindNum(Double.valueOf(format11));

                        if (null != lastOSgappStat){
                            long l2 = lastOSgappStat.getSgappTRegNum() + format7 * count;
                            oIndexb.settRegNum(Double.valueOf(l2));
                            long l = lastOSgappStat.getSgappTBindNum() + format8 * count;
                            oIndexb.settBindNum(Double.valueOf(l));
                            if (null != oPowerBindRateCity){
                                long l1 = (lastOSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind()) + format10 * count;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }else {
                                long l1 = lastOSgappStat.getHigBindNum() + format10 * count;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }
                        }else {
                            oIndexb.settRegNum(Double.valueOf(format7));
                            oIndexb.settBindNum(Double.valueOf(format8));
                            oIndexb.settFirmBindNum(Double.valueOf(format10));
                        }

                    }
                    oIndexBreakdownList.add(oIndexb);
                }

            }else {
                // 查询之前已完成的累计数
                // 各区县本月新增注册数=(新增注册数-当前已完成注册数)/要分解的月份 *户数占比

                Date endDate = DateUtils.getEndDayOfLastMonth();
                String lastMonthETime = DateUtils.longSdf.format(endDate);
                // 获取开始时间
                String strTime = lastMonthETime.substring(0,10) + " 00:00:00";

                /*String beforeDayTime = DateUtils.beforeDayTime();
                String strTime = beforeDayTime + " 00:00:00";
                String eTime = beforeDayTime + " 23:59:59";*/
                // 查询上个月的累计注册数和累计绑定数、高压用户绑定数
                OSgappStat oSgappStatCity = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,lastMonthETime);
                // 新增企业用户绑定数=(新增企业用户绑定数-当前已完成企业用户绑定数)/要分解的月份 * 户数占比
                OPowerBindRate oPowerBindRate = ioPowerBindRateService.selectOPowerBindRateByUnit(oIndexBreakdown.getUnit(),strTime,lastMonthETime);
                // 查询之前已完成的注册数和绑定数
                strTime = oIndexBreakdown.getYear() + "-01-01 00:00:00";
                OSgappStat addSgappCity = ioSgappStatService.countNewNumByUnit(oIndexBreakdown.getUnit(),strTime,lastMonthETime);
                // 查询去年年底的高压绑定数和非居民绑定数



                if (null != oSgappStatCity){
                    double v = oIndexBreakdown.gettRegNum() - oSgappStatCity.getSgappTRegNum();
                    oIndexBreakdown.settRegNum(v);
                    double v1 = oIndexBreakdown.gettBindNum() - oSgappStatCity.getSgappTBindNum();
                    oIndexBreakdown.settBindNum(v1);
                    if (null != oPowerBindRate){
                        double v2 = oIndexBreakdown.gettFirmBindNum() - (oSgappStatCity.getHigBindNum() + oPowerBindRate.getNoresideConsBind());
                        oIndexBreakdown.settFirmBindNum(v2);
                    }else {
                        double v2 = oIndexBreakdown.gettFirmBindNum() - oSgappStatCity.getHigBindNum();
                        oIndexBreakdown.settFirmBindNum(v2);
                    }
                    if (null != addSgappCity){
                        double v3 = oIndexBreakdown.getnRegNum() - addSgappCity.getSgappDRegNum();
                        oIndexBreakdown.setnRegNum(v3);
                        double v4 = oIndexBreakdown.getnBindNum() - addSgappCity.getSgappDBindNum();
                        oIndexBreakdown.setnBindNum(v4);
                        // 计算当前已完成的企业绑定数  = 上个月的企业绑定数 - 去年底的企业绑定数
                        long tfirmbindNum = (oSgappStatCity.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) - (lastOSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind());
                        //  全市的新增企业绑定数 - 已完成的企业绑定数 = 真正的新增企业绑定数目标值
                        double v5 = oIndexBreakdown.getnFirmBindNum() - tfirmbindNum;
                        oIndexBreakdown.setnFirmBindNum(v5);
                    }
                }

                double nregNum = oIndexBreakdown.getnRegNum() / selectTimeList.size();
                int format6 = (int) nregNum;
                double v = oIndexBreakdown.getnRegNum() % selectTimeList.size();
                int v1 = (int) v;
                double tregNum = oIndexBreakdown.gettRegNum() / selectTimeList.size();
                int format7 = (int) tregNum;
                double v2 = oIndexBreakdown.gettRegNum() % selectTimeList.size();
                int v3 = (int) v2;
                double tbindNum = oIndexBreakdown.gettBindNum() / selectTimeList.size();
                int format8 = (int) tbindNum;
                double v4 = oIndexBreakdown.gettBindNum() % selectTimeList.size();
                int v5 = (int) v4;
                double nbindNum = oIndexBreakdown.getnBindNum() / selectTimeList.size();
                int format9 = (int) nbindNum;
                double v6 = oIndexBreakdown.getnBindNum() % selectTimeList.size();
                int v7 = (int) v6;

                double tfirmBindNum = oIndexBreakdown.gettFirmBindNum() / selectTimeList.size();
                int format10 = (int) tfirmBindNum;
                double v8 = oIndexBreakdown.gettFirmBindNum() % selectTimeList.size();
                int v9 = (int) v8;
                double nfirmBindNum = oIndexBreakdown.getnFirmBindNum() / selectTimeList.size();
                int format11 = (int) nfirmBindNum;
                double v10 = oIndexBreakdown.getnFirmBindNum() % selectTimeList.size();
                int v11 = (int) v10;
                int count = 0;
                for(String breakTime : selectTimeList){
                    count ++;
                    OIndexBreakdown oIndexb  = new OIndexBreakdown();
                    oIndexb.setUnit(oIndexBreakdown.getUnit());
                    oIndexb.setYear(oIndexBreakdown.getYear());
                    oIndexb.setPeriod(breakTime);
                    if (count == selectTimeList.size()) {
                        int i = format6 + v1;
                        oIndexb.setnRegNum(Double.valueOf(i));
                        int i1 = format7 + v3;
                        int i2 = format8 + v5;
                        int i4 = format10 + v9;
                        if (null != oSgappStatCity) {
                            long v12 = oSgappStatCity.getSgappTRegNum() + format7 * count + v3;
                            oIndexb.settRegNum(Double.valueOf(v12));
                            long l = oSgappStatCity.getSgappTBindNum() + format8 * count + v5;
                            oIndexb.settBindNum(Double.valueOf(l));
                            if (null != oPowerBindRate){
                                long l1 = (oSgappStatCity.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) + format10 * count + v9;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }else {
                                long l1 = oSgappStatCity.getHigBindNum() + format10 * count + v9;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }
                        } else {
                            oIndexb.settRegNum(Double.valueOf(i1));
                            oIndexb.settBindNum(Double.valueOf(i2));
                            oIndexb.settFirmBindNum(Double.valueOf(i4));
                        }
                        int i3 = format9 + v7;
                        oIndexb.setnBindNum(Double.valueOf(i3));
                        int i5 = format11 + v11;
                        oIndexb.setnFirmBindNum(Double.valueOf(i5));
                    }else {
                        oIndexb.setnRegNum(Double.valueOf(format6));
                        oIndexb.setnBindNum(Double.valueOf(format9));
                        oIndexb.setnFirmBindNum(Double.valueOf(format11));

                        if (null != oSgappStatCity){
                            long l2 = oSgappStatCity.getSgappTRegNum() + format7 * count;
                            oIndexb.settRegNum(Double.valueOf(l2));
                            long l = oSgappStatCity.getSgappTBindNum() + format8 * count;
                            oIndexb.settBindNum(Double.valueOf(l));
                            if (null != oPowerBindRate){
                                long l1 = (oSgappStatCity.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) + format10 * count;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }else {
                                long l1 = oSgappStatCity.getHigBindNum() + format10 * count;
                                oIndexb.settFirmBindNum(Double.valueOf(l1));
                            }
                        }else {
                            oIndexb.settRegNum(Double.valueOf(format7));
                            oIndexb.settBindNum(Double.valueOf(format8));
                            oIndexb.settFirmBindNum(Double.valueOf(format10));
                        }
                    }


                    oIndexBreakdownList.add(oIndexb);
                }
            }
            return oIndexBreakdownList;

        }
        return null;
    }

    @Override
    public List<OIndexBreakdown> getIndexBrekdownByUnit(OIndexBreakdown oIndexBreakdown) throws Exception {
        // 如果是省 就查询出所有的地市，如果不是省，就查询自己
        DecimalFormat    df   = new DecimalFormat("######0.0000");
        List<String> orgNoList = new ArrayList<>();
        String lastYear = DateUtils.getLastYear();
        String sTime = lastYear + "-12-31 00:00:00";
        String eTime = lastYear + "-12-31 23:59:59";
        if ("35101".equals(oIndexBreakdown.getUnit())){
                if (null == oIndexBreakdown.getIsCounty() || "0".equals(oIndexBreakdown.getIsCounty())){
                    // orgNoList.add(oIndexBreakdown.getUnit());
                    R r = remoteDeptService.getUnit(oIndexBreakdown.getUnit());
                    if (200 == r.getCode()) {
                        List<SysDept> sysDeptList = getSysDepts(r);
                        for (SysDept sysDept : sysDeptList) {
                            orgNoList.add(sysDept.getDeptId());
                        }
                    }
                     /*
                     *  查询统计的该单位上一年的非企业用户数和企业用户数
                    * */
                    List<OConsPercentage> oConsPercentageList = ioConsPercentageService.selectOConsPercentageByunitAndTimeList(orgNoList,sTime,eTime);
                    List<OIndexBreakdown> indexBrekdownByUnitList = oIndexBreakdownMapper.getIndexBrekdownByUnit("0", oIndexBreakdown.getYear(), orgNoList);
                    for (OIndexBreakdown oIndexBreakdown1 : indexBrekdownByUnitList){
                        for (OConsPercentage oConsPercentage : oConsPercentageList){
                            if (oIndexBreakdown1.getUnit().equals(oConsPercentage.getUnit())){
                                if (null == oConsPercentage.getUserNum() || 0 == oConsPercentage.getUserNum()){
                                    oIndexBreakdown1.settRegRate(0.0);
                                    oIndexBreakdown1.settBindRate(0.0);
                                }else {
                                    double v = oIndexBreakdown1.gettRegNum() / oConsPercentage.getUserNum();
                                    String format = df.format(v);
                                    oIndexBreakdown1.settRegRate(Double.valueOf(format));
                                    double v1 = oIndexBreakdown1.gettBindNum() / oConsPercentage.getUserNum();
                                    String format1 = df.format(v1);
                                    oIndexBreakdown1.settBindRate(Double.valueOf(format1));
                                }
                                if(null == oConsPercentage.getQyUserNum() || 0 == oConsPercentage.getQyUserNum()){
                                    oIndexBreakdown1.settFirmRate(0.0);
                                }else {
                                    double v2 = oIndexBreakdown1.gettFirmBindNum() / oConsPercentage.getQyUserNum();
                                    String format2 = df.format(v2);
                                    oIndexBreakdown1.settFirmRate(Double.valueOf(format2));
                                }

                            }
                        }
                    }
                    return indexBrekdownByUnitList;
                }else {
                    R r = remoteDeptService.getCityAndQuXianBySheng(oIndexBreakdown.getUnit());
                    if (200 == r.getCode()) {
                        List<SysDept> sysDeptList = getSysDepts(r);
                        for (SysDept sysDept : sysDeptList) {
                            orgNoList.add(sysDept.getDeptId());
                        }
                    }
                    // List<CCons> cConsList = cConsMapper.selectCConsCountByCityList(orgNoList);
                     /*
                     *  查询统计的该单位上一年的非企业用户数和企业用户数
                    * */
                    List<OConsPercentage> oConsPercentageList = ioConsPercentageService.selectOConsPercentageByunitAndTimeList(orgNoList,sTime,eTime);
                    List<OIndexBreakdown> indexBrekdownByUnit = oIndexBreakdownMapper.getIndexBrekdownByUnit(oIndexBreakdown.getIsCounty(), oIndexBreakdown.getYear(), orgNoList);
                    for (OIndexBreakdown oIndexBreakdown1 : indexBrekdownByUnit){
                        for (OConsPercentage oConsPercentage : oConsPercentageList){
                            if (oIndexBreakdown1.getUnit().equals(oConsPercentage.getUnit())){
                                if (null == oConsPercentage.getUserNum() || 0 == oConsPercentage.getUserNum()){
                                    oIndexBreakdown1.settRegRate(0.0);
                                    oIndexBreakdown1.settBindRate(0.0);
                                }else {
                                    double v = oIndexBreakdown1.gettRegNum() / oConsPercentage.getUserNum();
                                    String format = df.format(v);
                                    oIndexBreakdown1.settRegRate(Double.valueOf(format));
                                    double v1 = oIndexBreakdown1.gettBindNum() / oConsPercentage.getUserNum();
                                    String format1 = df.format(v1);
                                    oIndexBreakdown1.settBindRate(Double.valueOf(format1));
                                }
                                if(null == oConsPercentage.getQyUserNum() || 0 == oConsPercentage.getQyUserNum()){
                                    oIndexBreakdown1.settFirmRate(0.0);
                                }else {
                                    double v2 = oIndexBreakdown1.gettFirmBindNum() / oConsPercentage.getQyUserNum();
                                    String format2 = df.format(v2);
                                    oIndexBreakdown1.settFirmRate(Double.valueOf(format2));
                                }

                            }
                        }
                    }
                    return indexBrekdownByUnit;
                }
        }else {
            if (null == oIndexBreakdown.getIsCounty() || "0".equals(oIndexBreakdown.getIsCounty())){
                List<OIndexBreakdown> indexBrekdownByUnitCity = oIndexBreakdownMapper.getIndexBrekdownByUnitCity("0", oIndexBreakdown.getYear(), oIndexBreakdown.getUnit(), orgNoList);
                for(OIndexBreakdown oIndexBreakdown1 : indexBrekdownByUnitCity){
                    /*
                        *  查询统计的该单位上一年的非企业用户数和企业用户数
                        * */
                    OConsPercentage oConsPercentage = ioConsPercentageService.selectOConsPercentageByunitAndTime(oIndexBreakdown1.getUnit(),sTime,eTime);
                    if (null != oConsPercentage && null != oConsPercentage.getUserNum() && oConsPercentage.getUserNum() != 0) {
                        double v = oIndexBreakdown1.gettRegNum() / oConsPercentage.getUserNum();
                        String format = df.format(v);
                        oIndexBreakdown1.settRegRate(Double.valueOf(format));
                        double v1 = oIndexBreakdown1.gettBindNum() / oConsPercentage.getUserNum();
                        String format1 = df.format(v1);
                        oIndexBreakdown1.settBindRate(Double.valueOf(format1));
                    } else{
                        oIndexBreakdown1.settRegRate(0.0);
                        oIndexBreakdown1.settBindRate(0.0);
                    }
                    if (null != oConsPercentage && null != oConsPercentage.getQyUserNum() && oConsPercentage.getQyUserNum() != 0){
                        double v2 = oIndexBreakdown1.gettFirmBindNum() / oConsPercentage.getQyUserNum();
                        String format2 = df.format(v2);
                        oIndexBreakdown1.settFirmRate(Double.valueOf(format2));
                    } else {
                        oIndexBreakdown1.settFirmRate(0.0);
                    }
                }
                return indexBrekdownByUnitCity;
            }else {
                R r = remoteDeptService.getUnit(oIndexBreakdown.getUnit());
                List data = (List) r.getData();
                if (200 == r.getCode() && data.size() > 0) {
                    List<SysDept> sysDeptList = getSysDepts(r);
                    for (SysDept sysDept : sysDeptList) {
                        orgNoList.add(sysDept.getDeptId());
                    }
                }else {
                    orgNoList.add(oIndexBreakdown.getUnit());
                }
                /*List<CCons> cConsList = cConsMapper.selectCConsCountByCityList(orgNoList);*/
                /*
                     *  查询统计的该单位上一年的非企业用户数和企业用户数
                    * */
                List<OConsPercentage> oConsPercentageList = ioConsPercentageService.selectOConsPercentageByunitAndTimeList(orgNoList,sTime,eTime);
                List<OIndexBreakdown> indexBrekdownByUnitCity = oIndexBreakdownMapper.getIndexBrekdownByUnitCity(oIndexBreakdown.getIsCounty(), oIndexBreakdown.getYear(), oIndexBreakdown.getUnit(), orgNoList);
                for(OIndexBreakdown oIndexBreakdown1 : indexBrekdownByUnitCity){
                    for (OConsPercentage oConsPercentage : oConsPercentageList){
                        if (oIndexBreakdown1.getUnit().equals(oConsPercentage.getUnit())){
                            if (null == oConsPercentage.getUserNum() || 0 == oConsPercentage.getUserNum()){
                                oIndexBreakdown1.settRegRate(0.0);
                                oIndexBreakdown1.settBindRate(0.0);
                            }else{
                                double v = oIndexBreakdown1.gettRegNum() / oConsPercentage.getUserNum();
                                String format = df.format(v);
                                oIndexBreakdown1.settRegRate(Double.valueOf(format));
                                double v1 = oIndexBreakdown1.gettBindNum() / oConsPercentage.getUserNum();
                                String format1 = df.format(v1);
                                oIndexBreakdown1.settBindRate(Double.valueOf(format1));
                            }
                            if (null == oConsPercentage.getQyUserNum() || 0 == oConsPercentage.getQyUserNum()){
                                oIndexBreakdown1.settFirmRate(0.0);
                            } else {
                                double v2 = oIndexBreakdown1.gettFirmBindNum() / oConsPercentage.getQyUserNum();
                                String format2 = df.format(v2);
                                oIndexBreakdown1.settFirmRate(Double.valueOf(format2));
                            }
                        }
                    }
                }
                return indexBrekdownByUnitCity;
            }
        }
    }

    @Override
    public List<IndicatorExecutionDto> implementationIndicators(String orgNo, String year) throws Exception {

        List<String> orgNoList = new ArrayList<>();
        if("35101".equals(orgNo)){
            R r = remoteDeptService.getUnit(orgNo);
            if (null != r && 200 == r.getCode()) {
                List<SysDept> sysDeptList = getSysDepts(r);
                for (SysDept sysDept : sysDeptList){
                    orgNoList.add(sysDept.getDeptId());
                }
            }
        }else {
            orgNoList.add(orgNo);
        }
            List<IndicatorExecutionDto> indicatorExecutionDtoList1 = null;
            // 查询目标值
            if (orgNoList.size() == 1){
                indicatorExecutionDtoList1 = oIndexBreakdownMapper.implementationIndQuXian(orgNo, year);
            }else {
                indicatorExecutionDtoList1 = oIndexBreakdownMapper.implementationIndicators(orgNoList, year, orgNo);
            }
            // 处理double类型的数为int
            for(IndicatorExecutionDto indicatorExecutionDto : indicatorExecutionDtoList1){
                double aDouble = Double.valueOf(indicatorExecutionDto.getnRegMbNum());
                Integer nRegNum = (int) aDouble;
                indicatorExecutionDto.setnRegMbNum(nRegNum.toString());
                double aDouble1 = Double.valueOf(indicatorExecutionDto.getnBindMbNum());
                Integer nBindNum = (int) aDouble1;
                indicatorExecutionDto.setnBindMbNum(nBindNum.toString());
                double aDouble2 = Double.valueOf(indicatorExecutionDto.getnFirmMbNum());
                Integer nFirmNum = (int) aDouble2;
                indicatorExecutionDto.setnFirmMbNum(nFirmNum.toString());
            }
            // 查询指标值
            List<IndicatorExecutionDto> indicatorExecutionDtoList2 = new ArrayList<>();
            for (int i=1; i<=12; i++){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 获取所选年的每个月最后一天的开始时间和结束时间
                Date endTime = DateUtils.getEndTime(Integer.valueOf(year), i);
                String endtime = sdf.format(endTime);
                String starttime = endtime.substring(0,10) + " 00:00:00";

                if (orgNoList.size() <= 0){
                    orgNoList.add(orgNo);
                }
                // 查询新增注册数指标值和新增绑定数指标值
                IndicatorExecutionDto indicatorExecutionDtOver = ioSgappStatService.implementationIndicators(orgNoList,starttime,endtime);
                if (null != indicatorExecutionDtOver){
                    indicatorExecutionDtOver.setMonth(i+"");
                }
                // 查询新增企业绑定数指标值
                // 获取所选年的每个月第一天的开始时间和结束时间
                if (i == 1){
                    endTime = DateUtils.getEndTime(Integer.valueOf(year) - 1, 12);
                    endtime = sdf.format(endTime);
                    starttime = endtime.substring(0,10) + " 00:00:00";
                }else {
                    endTime = DateUtils.getEndTime(Integer.valueOf(year), i - 1);
                    endtime = sdf.format(endTime);
                    starttime = endtime.substring(0,10) + " 00:00:00";
                }
                IndicatorExecutionDto indicatorExecutionDtoFirst = ioSgappStatService.implementationIndicators(orgNoList,starttime,endtime);
                // 新增企业绑定数指标值 = 本月底企业累计绑定数 - 上月底企业累计绑定数
                if (null != indicatorExecutionDtOver && null != indicatorExecutionDtoFirst){
                    int i1 = Integer.valueOf(indicatorExecutionDtOver.getnFirmZbNum()) - Integer.valueOf(indicatorExecutionDtoFirst.getnFirmZbNum());
                    indicatorExecutionDtOver.setnFirmZbNum(i1+"");
                }
                if (null != indicatorExecutionDtOver){
                    indicatorExecutionDtoList2.add(indicatorExecutionDtOver);
                }
                /*else {
                    // TODO 计算预测值
                    IndicatorExecutionDto indicatorExecutionDto1 = new IndicatorExecutionDto();
                    IndicatorExecutionDto indicatorExecutionDto2 = new IndicatorExecutionDto();
                    if (i == 1){
                        Date beginTime1 = DateUtils.getBeginTime(Integer.valueOf(year) - 1, i);
                        String starttime1 = sdf.format(beginTime1);
                        Date endTime1 = DateUtils.getEndTime(Integer.valueOf(year) - 1, i);
                        String endtime1 = sdf.format(endTime1);
                        // 查询指标值
                        IndicatorExecutionDto lastyearDto = ioSgappStatService.implementationIndicators(orgNoList,starttime1,endtime1);
                        if (null != lastyearDto){
                            indicatorExecutionDto1 = lastyearDto;
                            indicatorExecutionDto2 = lastyearDto;
                        }else {
                            // TODO 若去年同期没有增长值，则取目标值
                            String month = "0" + i;
                            for(IndicatorExecutionDto indicatorExecutionDto3 : indicatorExecutionDtoList1){
                                if (month.equals(indicatorExecutionDto3.getMonth())){
                                    indicatorExecutionDto1.setnRegYcNum(indicatorExecutionDto3.getnRegMbNum());
                                    indicatorExecutionDto1.setnBindYcNum(indicatorExecutionDto3.getnBindYcNum());
                                    indicatorExecutionDto1.setnFirmYcNum(indicatorExecutionDto3.getnFirmYcNum());
                                    indicatorExecutionDto2.setnRegYcNum(indicatorExecutionDto3.getnRegMbNum());
                                    indicatorExecutionDto2.setnBindYcNum(indicatorExecutionDto3.getnBindYcNum());
                                    indicatorExecutionDto2.setnFirmYcNum(indicatorExecutionDto3.getnFirmYcNum());
                                    break;
                                }
                            }
                        }
                    }else if(i == 2) {
                        if (indicatorExecutionDtoList2.size() > 0){
                            // 本期
                            indicatorExecutionDto1 = indicatorExecutionDtoList2.get(0);
                            // 上期
                            indicatorExecutionDto2 = indicatorExecutionDtoList2.get(0);
                        }
                    }else if(i == 3){
                        if (indicatorExecutionDtoList2.size() >= 2) {
                            // 本期
                            indicatorExecutionDto1 = indicatorExecutionDtoList2.get(i - 1);
                            // 上期
                            indicatorExecutionDto2 = indicatorExecutionDtoList2.get(i - 2);
                        }
                    }else {
                        if (indicatorExecutionDtoList2.size() >= 3) {
                            int i1 = i - 2;
                            if(i1 < indicatorExecutionDtoList2.size()){
                                // 本期
                                indicatorExecutionDto1 = indicatorExecutionDtoList2.get(i - 2);
                            }
                            int i2 = i - 3;
                            if (i2 < indicatorExecutionDtoList2.size()){
                                // 上期
                                indicatorExecutionDto2 = indicatorExecutionDtoList2.get(i - 3);
                            }
                        }
                    }

                    if (indicatorExecutionDto1.getnRegZbNum() != null && indicatorExecutionDto2.getnRegZbNum() != null){
                        IndicatorExecutionDto indicatorExecutionDto3 = new IndicatorExecutionDto();
                        if (i <10){
                            indicatorExecutionDto3.setMonth("0"+i);
                        }else {
                            indicatorExecutionDto3.setMonth(i+"");
                        }

                        if (Double.parseDouble(indicatorExecutionDto1.getnRegZbNum()) <= Double.parseDouble(indicatorExecutionDto2.getnRegZbNum())){
                            long nRegYcNum = Math.round(Double.parseDouble(indicatorExecutionDto1.getnRegZbNum()) * (1 + 0.1));
                            long nBindYcNum = Math.round(Double.parseDouble(indicatorExecutionDto1.getnBindZbNum()) * (1 + 0.1));
                            long nFirmZbNum = Math.round(Double.parseDouble(indicatorExecutionDto1.getnFirmZbNum()) * (1 + 0.1));

                            indicatorExecutionDto3.setnRegYcNum(nRegYcNum+"");
                            indicatorExecutionDto3.setnBindYcNum(nBindYcNum+"");
                            indicatorExecutionDto3.setnFirmYcNum(nFirmZbNum+"");

                        }else {
                            double i1 = (Double.parseDouble(indicatorExecutionDto1.getnRegZbNum()) - Double.parseDouble(indicatorExecutionDto2.getnRegZbNum())) / Double.parseDouble(indicatorExecutionDto2.getnRegZbNum());
                            long v1 = Math.round(i1);
                            double d1 = Double.parseDouble(indicatorExecutionDto2.getnRegZbNum()) * (1 + v1);
                            long nRegYcNum = Math.round(d1);

                            double i2 = (Double.parseDouble(indicatorExecutionDto1.getnBindZbNum()) - Double.parseDouble(indicatorExecutionDto2.getnBindZbNum())) / Double.parseDouble(indicatorExecutionDto2.getnBindZbNum());
                            long v2 = Math.round(i2);
                            double d2 = Double.parseDouble(indicatorExecutionDto2.getnRegZbNum()) * (1 + v2);
                            long nBindYcNum = Math.round(d2);

                            double i3 = (Double.parseDouble(indicatorExecutionDto1.getnFirmZbNum()) - Double.parseDouble(indicatorExecutionDto2.getnFirmZbNum())) / Double.parseDouble(indicatorExecutionDto2.getnFirmZbNum());
                            long v3 = Math.round(i3);
                            double d3 = Double.parseDouble(indicatorExecutionDto2.getnRegZbNum()) * (1 + v3);
                            long nFirmZbNum = Math.round(d3);

                            indicatorExecutionDto3.setnRegYcNum(nRegYcNum+"");
                            indicatorExecutionDto3.setnBindYcNum(nBindYcNum+"");
                            indicatorExecutionDto3.setnFirmYcNum(nFirmZbNum+"");
                        }
                        // indicatorExecutionDto3.setUnit();
                        indicatorExecutionDtoList2.add(indicatorExecutionDto3);
                    }
                }*/
            }
            for (IndicatorExecutionDto indicatorExecutionDto : indicatorExecutionDtoList1){
                indicatorExecutionDtoList2.add(indicatorExecutionDto);
                /*for (IndicatorExecutionDto indicatorExecutionDto1 : indicatorExecutionDtoList2){
                        indicatorExecutionDto1.setnRegMbNum(indicatorExecutionDto.getnRegMbNum());
                        indicatorExecutionDto1.setnBindMbNum(indicatorExecutionDto.getnBindMbNum());
                        indicatorExecutionDto1.setnFirmMbNum(indicatorExecutionDto.getnFirmMbNum());
                }*/
            }
            // indicatorExecutionDtoList1.addAll(indicatorExecutionDtoList2);
            return indicatorExecutionDtoList2;

    }

    @Override
    public List<IndicatorExecutionDto> addNumPercentage(String orgNo, String year) throws Exception {
        // 查询当前地区的下级地区
        R r = remoteDeptService.getUnit(orgNo);
        /*if("35101".equals(orgNo)){
            r = remoteDeptService.getUnit(orgNo);
        }else {
            r = remoteDeptService.getSubordinateUnit(orgNo);
        }*/

        List<String> orgNoList = new ArrayList<>();
        List<IndicatorExecutionDto> indicatorExecutionDto = new ArrayList<>();
        if (null != r && 200 == r.getCode()){
            List list = (List)r.getData();
            if(list.size() < 1){
                orgNoList.add(orgNo);
                // 查询出各区县的新增注册数和新绑定数
                indicatorExecutionDto = oIndexBreakdownMapper.isContainAddNumPercentage(orgNoList,orgNo,year);
                return indicatorExecutionDto;
            }
            List<SysDept> sysDeptList = getSysDepts(r);
            for (SysDept sysDept : sysDeptList){
                orgNoList.add(sysDept.getDeptId());
            }
            if ("35101".equals(orgNo)){
                // 查询出各地市的新增注册数和新绑定数
                indicatorExecutionDto = oIndexBreakdownMapper.addNumPercentage(orgNoList,year);
            }else {
                if(orgNoList.size() < 1){
                    orgNoList.add(orgNo);
                }
                // 查询出各区县的新增注册数和新绑定数
                indicatorExecutionDto = oIndexBreakdownMapper.isContainAddNumPercentage(orgNoList,orgNo,year);
            }
        }else {
            if(orgNoList.size() < 1){
                orgNoList.add(orgNo);
            }
            // 查询出各区县的新增注册数和新绑定数
            indicatorExecutionDto = oIndexBreakdownMapper.isContainAddNumPercentage(orgNoList,orgNo,year);
        }
        return indicatorExecutionDto;
    }

    @Override
    public OIndexBreakdown getIndexBrekdownByUnit(String year, String unit) {
        OIndexBreakdown oIndexBreakdown = oIndexBreakdownMapper.selectOIndexBreakdownByYearAndUnit(unit,year,null);
        return oIndexBreakdown;
    }

    @Override
    public List<OIndexBreakdown> getIndexDetailList(String orgNo, String type, String startTime, String endTime) throws Exception {
        // 得到要统计的月份
        String month = "";
        String year = "";
        if (null != endTime){
            endTime = DateUtils.judgeStatTime(endTime);
            String substring = endTime.substring(5, 7);
            month = Integer.valueOf(substring) + "";
            year = endTime.substring(0,4);
        }
        List<OIndexBreakdown> aLLYearoIndexBreakdownList = new ArrayList<OIndexBreakdown>();
        // 如果是省公司，查询九地市的数据
        if ("35101".equals(orgNo)){
            // 当年目标值
            aLLYearoIndexBreakdownList = oIndexBreakdownMapper.getIndexDetailList(year);
        }else {
            //  地市区县，查询本级或下级
            // 当年目标值
            aLLYearoIndexBreakdownList = oIndexBreakdownMapper.getCityIndexDetailList(orgNo,year,month);
        }
        String strTime = "";
        String eTime = endTime + " 23:59:59";
        DecimalFormat    df   = new DecimalFormat("######0.0000");
        if (aLLYearoIndexBreakdownList.size() > 0){
            for (OIndexBreakdown oIndexBreakdown : aLLYearoIndexBreakdownList){

                // 查询当前地市的当前指标值
                /*strTime = DateUtils.beforeDayTime() + " 00:00:00";
                eTime = DateUtils.beforeDayTime() + " 23:59:59";*/
                strTime = endTime + " 00:00:00";
                eTime = endTime + " 23:59:59";
                OSgappStat oSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                // 本月目标值
                OIndexBreakdown cityOIndexCurrent = oIndexBreakdownMapper.getIndexByUnitAndYearAndMonth(oIndexBreakdown.getUnit(), year, month);
                oIndexBreakdown.setStatisticsPeriod("日");
                oIndexBreakdown.setStatisticsTime(endTime);
                oIndexBreakdown.setType(type);
                if (null != oSgappStat){
                    // 月初指标值 就是查询上月底完成的值
                    Date endDayOfLastMonth = DateUtils.getEndDayOfLastMonth();
                    String endDate = endDayOfLastMonth.toString();
                    strTime = endDate.substring(0, 10) + " 00:00:00";
                    eTime = endDate.substring(0, 19);
                    OSgappStat chuoSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                    if ("1".equals(type)){
                        oIndexBreakdown.setCurrentValue(oSgappStat.getSgappTRegNum());
                        // 全目标值
                        if (null != oIndexBreakdown.gettRegNum()){
                            double aDouble = oIndexBreakdown.gettRegNum();
                            Integer tRegNum = (int) aDouble;
                            if (null == tRegNum){
                                oIndexBreakdown.setAllValue("0");
                            }else {
                                oIndexBreakdown.setAllValue(tRegNum.toString());
                            }
                        }else{
                            oIndexBreakdown.setAllValue("0");
                        }


                        // 全年完成率
                        if (null != oIndexBreakdown.gettRegNum()){
                            double v = oSgappStat.getSgappTRegNum() / oIndexBreakdown.gettRegNum();
                            String format = df.format(v);
                            oIndexBreakdown.setAllYearWcRatio(Double.valueOf(format));
                        }else {
                            oIndexBreakdown.setAllYearWcRatio(0.0);
                        }

                        // 本月目标值
                        if (null != cityOIndexCurrent && null != cityOIndexCurrent.gettRegNum()){
                            oIndexBreakdown.setThisMonthValue(cityOIndexCurrent.gettRegNum());
                            // 本月完成比例 = （当前指标值 - 月初的指标值） / 本月目标值
                            if (null != chuoSgappStat){
                                double v2 = (oSgappStat.getSgappTRegNum() - chuoSgappStat.getSgappTRegNum()) / cityOIndexCurrent.gettRegNum();
                                String format1 = df.format(v2);
                                oIndexBreakdown.setThisMonthWcRation(Double.valueOf(format1));
                            }
                        }else {
                            oIndexBreakdown.setThisMonthWcRation(0.0);
                        }
                        // 实际指标值
                        strTime = DateUtils.beforeDayTime() + " 00:00:00";
                        eTime = DateUtils.beforeDayTime() + " 23:59:59";
                        OSgappStat shijioSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                        if (null != shijioSgappStat){
                            oIndexBreakdown.setActualValue(shijioSgappStat.getSgappTRegNum().intValue());
                        }else {
                            oIndexBreakdown.setActualValue(0);
                        }

                    }else if ("2".equals(type)){
                        oIndexBreakdown.setCurrentValue(oSgappStat.getSgappDRegNum());
                        // 全目标值
                        if (null != oIndexBreakdown.getnRegNum()){
                            double aDouble = oIndexBreakdown.getnRegNum();
                            Integer nRegNum = (int) aDouble;
                            oIndexBreakdown.setAllValue(nRegNum.toString());
                        }else {
                            oIndexBreakdown.setAllValue("0");
                        }

                        // 全年完成率
                        if (null != oIndexBreakdown.getnRegNum() && 0.0 != oIndexBreakdown.getnRegNum()){
                            double v = oSgappStat.getSgappDRegNum() / oIndexBreakdown.getnRegNum();
                            String format = df.format(v);
                            oIndexBreakdown.setAllYearWcRatio(Double.valueOf(format));
                        }else {
                            oIndexBreakdown.setAllYearWcRatio(0.0);
                        }

                        // 本月目标值
                        if (null != cityOIndexCurrent && null != cityOIndexCurrent.getnRegNum() && 0.0 != cityOIndexCurrent.getnRegNum()){
                            oIndexBreakdown.setThisMonthValue(cityOIndexCurrent.getnRegNum());
                            // 本月完成比例 = （当前指标值 - 月初的指标值） / 本月目标值
                            if (null != chuoSgappStat){
                                double v2 = (oSgappStat.getSgappDRegNum() - chuoSgappStat.getSgappDRegNum()) / cityOIndexCurrent.getnRegNum();
                                String format1 = df.format(v2);
                                oIndexBreakdown.setThisMonthWcRation(Double.valueOf(format1));
                            }
                        }else {
                            oIndexBreakdown.setThisMonthWcRation(0.0);
                        }
                        // 实际指标值
                        strTime = DateUtils.beforeDayTime() + " 00:00:00";
                        eTime = DateUtils.beforeDayTime() + " 23:59:59";
                        OSgappStat shijioSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                        if (null != shijioSgappStat){
                            oIndexBreakdown.setActualValue(shijioSgappStat.getSgappDRegNum().intValue());
                        }else {
                            oIndexBreakdown.setActualValue(0);
                        }

                    }else if ("3".equals(type)){
                        oIndexBreakdown.setCurrentValue(oSgappStat.getSgappTBindNum());
                        // 全目标值
                        if (null != oIndexBreakdown.gettBindNum()){
                            double aDouble = oIndexBreakdown.gettBindNum();
                            Integer tBindNum = (int) aDouble;
                            oIndexBreakdown.setAllValue(tBindNum.toString());
                        }else {
                            oIndexBreakdown.setAllValue("0");
                        }

                        // 全年完成率
                        if (null != oIndexBreakdown.gettBindNum() && 0.0 != oIndexBreakdown.gettBindNum()){
                            double v = oSgappStat.getSgappTBindNum() / oIndexBreakdown.gettBindNum();
                            String format = df.format(v);
                            oIndexBreakdown.setAllYearWcRatio(Double.valueOf(format));
                        }else {
                            oIndexBreakdown.setAllYearWcRatio(0.0);
                        }

                        // 本月目标值
                        if (null != cityOIndexCurrent && null != cityOIndexCurrent.gettBindNum() && 0.0 != cityOIndexCurrent.gettBindNum()){
                            oIndexBreakdown.setThisMonthValue(cityOIndexCurrent.gettBindNum());
                            // 本月完成比例 = （当前指标值 - 月初的指标值） / 本月目标值
                            if (null != chuoSgappStat){
                                double v2 = (oSgappStat.getSgappTBindNum() - chuoSgappStat.getSgappTBindNum()) / cityOIndexCurrent.gettBindNum();
                                String format1 = df.format(v2);
                                oIndexBreakdown.setThisMonthWcRation(Double.valueOf(format1));
                            }
                        }else {
                            oIndexBreakdown.setThisMonthWcRation(0);
                        }
                        // 实际指标值
                        strTime = DateUtils.beforeDayTime() + " 00:00:00";
                        eTime = DateUtils.beforeDayTime() + " 23:59:59";
                        OSgappStat shijioSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                        if (null != shijioSgappStat){
                            oIndexBreakdown.setActualValue(shijioSgappStat.getSgappTBindNum().intValue());
                        }else {
                            oIndexBreakdown.setActualValue(0);
                        }
                    }else if ("4".equals(type)){
                        oIndexBreakdown.setCurrentValue(oSgappStat.getSgappDBindNum());
                        // 全目标值
                        if (null != oIndexBreakdown.getnBindNum()){
                            double aDouble = oIndexBreakdown.getnBindNum();
                            Integer nBindNum = (int) aDouble;
                            oIndexBreakdown.setAllValue(nBindNum.toString());
                        }else {
                            oIndexBreakdown.setAllValue("0");
                        }

                        // 全年完成率
                        if (null != oIndexBreakdown.getnBindNum() && 0.0 != oIndexBreakdown.getnBindNum()){
                            double v = oSgappStat.getSgappDBindNum() / oIndexBreakdown.getnBindNum();
                            String format = df.format(v);
                            oIndexBreakdown.setAllYearWcRatio(Double.valueOf(format));
                        }else {
                            oIndexBreakdown.setAllYearWcRatio(0.0);
                        }

                        // 本月目标值
                        if (null != cityOIndexCurrent && null != cityOIndexCurrent.getnBindNum() && 0.0 != cityOIndexCurrent.getnBindNum()){
                            oIndexBreakdown.setThisMonthValue(cityOIndexCurrent.getnBindNum());
                            // 本月完成比例 = （当前指标值 - 月初的指标值） / 本月目标值
                            if (null != chuoSgappStat){
                                double v2 = (oSgappStat.getSgappDBindNum() - chuoSgappStat.getSgappDBindNum()) / cityOIndexCurrent.getnBindNum();
                                String format1 = df.format(v2);
                                oIndexBreakdown.setThisMonthWcRation(Double.valueOf(format1));
                            }
                        }else {
                            oIndexBreakdown.setThisMonthWcRation(0.0);
                        }
                        // 实际指标值
                        strTime = DateUtils.beforeDayTime() + " 00:00:00";
                        eTime = DateUtils.beforeDayTime() + " 23:59:59";
                        OSgappStat shijioSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                        if (null != shijioSgappStat){
                            oIndexBreakdown.setActualValue(shijioSgappStat.getSgappDBindNum().intValue());
                        }else {
                            oIndexBreakdown.setActualValue(0);
                        }
                    }

                    if ("5".equals(type) || "6".equals(type)){
                        /*strTime = DateUtils.beforeDayTime() + " 00:00:00";
                        eTime = DateUtils.beforeDayTime() + " 23:59:59";*/
                        strTime = endTime + " 00:00:00";
                        eTime = endTime + " 23:59:59";
                        OPowerBindRate oPowerBindRate = ioPowerBindRateService.selectOPowerBindRateByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                        if (null != oPowerBindRate){

                            // 全目标值
                            if (null != oIndexBreakdown.gettFirmBindNum() && 0.0 != oIndexBreakdown.gettFirmBindNum()){
                                double aDouble = oIndexBreakdown.gettFirmBindNum();
                                Integer tFirmBindNum = (int) aDouble;
                                oIndexBreakdown.setAllValue(tFirmBindNum.toString());
                                // 全年完成率
                                double v = (oSgappStat.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) / oIndexBreakdown.gettFirmBindNum();
                                String format = df.format(v);
                                oIndexBreakdown.setAllYearWcRatio(Double.valueOf(format));
                            }else {
                                oIndexBreakdown.setAllValue("0");
                                oIndexBreakdown.setAllYearWcRatio(0.0);
                            }

                            // 本月目标值
                            if (null != cityOIndexCurrent && null != cityOIndexCurrent.gettFirmBindNum() && 0.0 != cityOIndexCurrent.gettFirmBindNum()){
                                oIndexBreakdown.setThisMonthValue(cityOIndexCurrent.gettFirmBindNum());
                                // 本月完成比例 = （当前指标值 - 月初的指标值） / 本月目标值
                                /*Date beginDayOfLastMonth = DateUtils.getBeginDayOfLastMonth();
                                String sDate = beginDayOfLastMonth.toString();*/
                                Date endDayOfLastMonth1 = DateUtils.getEndDayOfLastMonth();
                                String endDate1 = endDayOfLastMonth1.toString();
                                strTime = endDate1.substring(0, 10) + " 00:00:00";
                                eTime = endDate1.substring(0, 19);
                                OPowerBindRate lastMonthoPowerBind = ioPowerBindRateService.selectOPowerBindRateByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                                if (null != lastMonthoPowerBind){
                                    double v1 = (oPowerBindRate.getNoresideConsBind() - lastMonthoPowerBind.getNoresideConsBind()) / cityOIndexCurrent.gettFirmBindNum();
                                    String format1 = df.format(v1);
                                    oIndexBreakdown.setThisMonthWcRation(Double.valueOf(format1));
                                }
                            }else {
                                oIndexBreakdown.setThisMonthWcRation(0.0);
                            }
                            if ("6".equals(type)){
                                // 新增企业绑定数 = 当前时间的绑定数 - 去年年底的绑定数
                                String lastYear = DateUtils.getLastYear();
                                strTime = lastYear + "-12-31 00:00:00";
                                eTime = lastYear + "-12-31 23:59:59";
                                OPowerBindRate lastyearBindRate = ioPowerBindRateService.selectOPowerBindRateByUnit(orgNo,strTime,eTime);
                                OSgappStat lastyearoSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                                if (null != lastyearBindRate){
                                    double nFirmBindNum = 0.0;
                                    if (null != lastyearoSgappStat){
                                        nFirmBindNum = (oSgappStat.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) - (lastyearoSgappStat.getHigBindNum() + lastyearBindRate.getNoresideConsBind());
                                        oIndexBreakdown.setCurrentValue(nFirmBindNum);
                                    }else {
                                        nFirmBindNum = (oSgappStat.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) - lastyearBindRate.getNoresideConsBind();
                                        oIndexBreakdown.setCurrentValue(nFirmBindNum);
                                    }

                                    // 全目标值
                                    if (null != oIndexBreakdown.getnFirmBindNum() && 0.0 != oIndexBreakdown.getnFirmBindNum()){
                                        double aDoublen = oIndexBreakdown.getnFirmBindNum();
                                        Integer nFirmNum = (int) aDoublen;
                                        oIndexBreakdown.setAllValue(nFirmNum.toString());
                                        // 全年完成率
                                        double v1 = (oSgappStat.getHigBindNum() + oPowerBindRate.getNoresideConsBind()) / oIndexBreakdown.getnFirmBindNum();
                                        String format1 = df.format(v1);
                                        oIndexBreakdown.setAllYearWcRatio(Double.valueOf(format1));
                                    }else {
                                        oIndexBreakdown.setAllValue("0");
                                        oIndexBreakdown.setAllYearWcRatio(0.0);
                                    }

                                    // 本月目标值
                                    if (null != cityOIndexCurrent && null != cityOIndexCurrent.getnFirmBindNum() && 0.0 != cityOIndexCurrent.getnFirmBindNum()){
                                        oIndexBreakdown.setThisMonthValue(cityOIndexCurrent.getnFirmBindNum());
                                        // 本月完成比例 = （当前指标值 - 月初的指标值） / 本月目标值
                                        /*Date beginDayOfLastMonth = DateUtils.getBeginDayOfLastMonth();
                                        String sDate = beginDayOfLastMonth.toString();*/
                                        Date endDayOfLastMonth1 = DateUtils.getEndDayOfLastMonth();
                                        String endDate1 = endDayOfLastMonth1.toString();
                                        strTime = endDate1.substring(0, 10) + " 00:00:00";
                                        eTime = endDate1.substring(0, 19);
                                        OPowerBindRate lastMonthoPowerBind = ioPowerBindRateService.selectOPowerBindRateByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                                        if (null != lastMonthoPowerBind){
                                            double v2 = (nFirmBindNum - lastMonthoPowerBind.getNoresideConsBind()) / cityOIndexCurrent.gettFirmBindNum();
                                            String format2 = df.format(v2);
                                            oIndexBreakdown.setThisMonthWcRation(Double.valueOf(format2));
                                        }
                                    }else {
                                        oIndexBreakdown.setThisMonthWcRation(0.0);
                                    }
                                }
                            }
                            // 实际指标值
                            strTime = DateUtils.beforeDayTime() + " 00:00:00";
                            eTime = DateUtils.beforeDayTime() + " 23:59:59";
                            OSgappStat shijioSgappStat = ioSgappStatService.getOsgappStatByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                            strTime = DateUtils.beforeDayTime() + " 00:00:00";
                            eTime = DateUtils.beforeDayTime() + " 23:59:59";
                            OPowerBindRate shijioPowerBindRate = ioPowerBindRateService.selectOPowerBindRateByUnit(oIndexBreakdown.getUnit(),strTime,eTime);
                            if (null != shijioSgappStat && null != shijioPowerBindRate){
                                Long firmNum = shijioSgappStat.getHigBindNum() + shijioPowerBindRate.getNoresideConsBind();
                                oIndexBreakdown.setActualValue(firmNum.intValue());
                            }else {
                                oIndexBreakdown.setActualValue(0);
                            }
                        }
                    }
                }
            }
            aLLYearoIndexBreakdownList.sort(comparing(OIndexBreakdown::getAllYearWcRatio).reversed());

            return aLLYearoIndexBreakdownList;
        }
        return new ArrayList<OIndexBreakdown>();
    }

    @Override
    public List<OIndexBreakdown> getShengBreakdownCityData(String year) {
        return oIndexBreakdownMapper.getShengBreakdownCityData(year);
    }

    @Override
    public List<OIndexBreakdown> districtBreakDown(List<OIndexBreakdown> oIndexBreakdownList) throws Exception {
        LoginUser loginUser = tokenService.getLoginUser();
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] json = objectMapper.writeValueAsBytes(oIndexBreakdownList);
        OIndexBreakdown[] oIndexBreakdowns = objectMapper.readValue(json, OIndexBreakdown[].class);
        List<OIndexBreakdown> oatdList = new ArrayList<>();
        Collections.addAll(oatdList, oIndexBreakdowns);

        // 先保存地市分解的数据
        for (OIndexBreakdown oIndexBreakdown : oatdList){
            // 先查询数据库是否有当前数据，有做修改操作，没有做保存操作
            OIndexBreakdown oIndexBreakdown1 = oIndexBreakdownMapper.selectOIndexBreakdownByYearAndUnit(oIndexBreakdown.getUnit(),oIndexBreakdown.getYear(),oIndexBreakdown.getPeriod());
            if (null != oIndexBreakdown1){
                oIndexBreakdown1.setUpdateTime(DateUtils.getNowDate());
                oIndexBreakdown1.setUpdateBy(loginUser.getUsername());
                oIndexBreakdown1.setStatus(oIndexBreakdown.getStatus());
                oIndexBreakdown1.settRegNum(oIndexBreakdown.gettRegNum());
                oIndexBreakdown1.setnRegNum(oIndexBreakdown.getnRegNum());
                oIndexBreakdown1.settBindNum(oIndexBreakdown.gettBindNum());
                oIndexBreakdown1.setnBindNum(oIndexBreakdown.getnBindNum());
                oIndexBreakdown1.settFirmBindNum(oIndexBreakdown.gettFirmBindNum());
                oIndexBreakdown1.setnFirmBindNum(oIndexBreakdown.getnFirmBindNum());
                oIndexBreakdownMapper.updateOIndexBreakdown(oIndexBreakdown1);
            }else {
                String id= UUID.randomUUID().toString().replaceAll("-", "");
                oIndexBreakdown.setId(id);
                oIndexBreakdown.setCreateTime(DateUtils.getNowDate());
                oIndexBreakdown.setCreateBy(loginUser.getUsername());
                oIndexBreakdown.setStatus(2);
                oIndexBreakdownMapper.insertOIndexBreakdown(oIndexBreakdown);
            }
        }
        List<OIndexBreakdown> oIndexBreakdownListOver = new ArrayList<>();
        //  查询当前地市下面的所有区县
        R r = remoteDeptService.getUnit(loginUser.getSysUser().getDeptId());
        List<SysDept> sysDeptList = new ArrayList<>();
        if (null != r && 200 == r.getCode()){
            List litst = (List) r.getData();
            if (litst.size() > 0){
                sysDeptList = getSysDepts(r);
            }else {
                SysDept sysDept = new SysDept();
                sysDept.setDeptId(loginUser.getSysUser().getDeptId());
                sysDeptList.add(sysDept);
            }
        }else{
            SysDept sysDept = new SysDept();
            sysDept.setDeptId(loginUser.getSysUser().getDeptId());
            sysDeptList.add(sysDept);
        }
        // 循环每个区县
        for (SysDept sysDept : sysDeptList){
            String orgNo = sysDept.getDeptId();
            // 查询当前区县的占比
            String lastYear = DateUtils.getLastYear();
            String sTime = lastYear + "-12-31 00:00:00";
            String eTime = lastYear + "-12-31 23:59:59";
            OConsPercentage oConsPercentage = ioConsPercentageService.selectOConsPercentageByunitAndTime(orgNo,sTime,eTime);
            // 获取结束时间
            Date endDate = DateUtils.getEndDayOfLastMonth();
            String endTime = DateUtils.longSdf.format(endDate);
            // 获取开始时间
            String startTime = endTime.substring(0,10) + " 00:00:00";
            // 获取上月底的累计绑定数数
            OSgappStat lastMonthOSgappStat = ioSgappStatService.getOsgappStatByUnit(orgNo,startTime,endTime);
            // 新增企业用户绑定数=(新增企业用户绑定数-当前已完成企业用户绑定数)/要分解的月份 * 户数占比
            // 获取上月企业累计绑定数
            OPowerBindRate lastMonthoPowerBindRate = ioPowerBindRateService.selectOPowerBindRateByUnit(orgNo,startTime,endTime);

            // 记录三个累计的数四舍五入后的值
            double zcsNum = 0.0;
            double bdsNum = 0.0;
            double qybdsNum = 0.0;
            // 计数
            int count = 0;
            // 循环地市每个月的指标
            for (OIndexBreakdown oIndexBreakdown : oatdList){
                count ++ ;

                OIndexBreakdown oIndexb  = new OIndexBreakdown();
                if (null != oConsPercentage && null != oConsPercentage.getPercentage()){
                    // 新增注册数
                    double nRegNum = oIndexBreakdown.getnRegNum() * oConsPercentage.getPercentage();
                    int nregNum1 = (int) nRegNum;
                    oIndexb.setnRegNum(Double.valueOf(nregNum1));
                    // 累计注册数   上月月底累计注册数+本月新增注册数
                    if (lastMonthOSgappStat != null){
                        double tRegNum = count * nregNum1 + lastMonthOSgappStat.getSgappTRegNum();
                        oIndexb.settRegNum(tRegNum);
                        /*if (count == oatdList.size()){
                            double tRegNum =count * nregNum1 + lastMonthOSgappStat.getSgappTRegNum();
                            zcsNum += tRegNum;
                            oIndexb.settRegNum(tRegNum);
                        }else {
                            double tRegNum = nregNum1 + zcsNum;
                            zcsNum += tRegNum;
                            oIndexb.settRegNum(tRegNum);
                        }*/
                    }
                    // 新增绑定数=(新增绑定数-当前已完成绑定数)/要分解的月份 *户数占比
                    double nBindNum = oIndexBreakdown.getnBindNum()* oConsPercentage.getPercentage();
                    int nBindNum1 = (int) nBindNum;
                    oIndexb.setnBindNum(Double.valueOf(nBindNum1));
                    // 累计绑定数
                    if (null != lastMonthOSgappStat){
                        double tBindNum = count * nBindNum1 + lastMonthOSgappStat.getSgappTBindNum();
                        oIndexb.settBindNum(tBindNum);
                        /*if (1 == count){
                            double tBindNum = nBindNum1 + lastMonthOSgappStat.getSgappTBindNum();
                            bdsNum += tBindNum;
                            oIndexb.settBindNum(tBindNum);
                        }else {
                            double tBindNum = nBindNum1 + bdsNum;
                            bdsNum += tBindNum;
                            oIndexb.settBindNum(tBindNum);
                        }*/

                    }
                    // 企业绑定数
                    if (null != lastMonthoPowerBindRate){
                        double nFirmBindNum = oIndexBreakdown.getnFirmBindNum() * oConsPercentage.getPercentage();
                        int nFirmBindNum1 = (int) nFirmBindNum;

                        oIndexb.setnFirmBindNum(Double.valueOf(nFirmBindNum1));
                        // 累计企业用户绑定数=上月月底累计注册数+本月新增注册数
                        if (null != lastMonthOSgappStat){
                            double tFirmBindNum = count * nFirmBindNum1 + lastMonthOSgappStat.getHigBindNum() + lastMonthoPowerBindRate.getNoresideConsBind();
                            oIndexb.settFirmBindNum(tFirmBindNum);

                            /*if (1 == count){
                                double tFirmBindNum = nFirmBindNum1 + lastMonthOSgappStat.getHigBindNum() + lastMonthoPowerBindRate.getNoresideConsBind();
                                qybdsNum += tFirmBindNum;
                                oIndexb.settFirmBindNum(tFirmBindNum);
                            }else {
                                double tFirmBindNum = nFirmBindNum1 + qybdsNum;
                                qybdsNum += tFirmBindNum;
                                oIndexb.settFirmBindNum(tFirmBindNum);
                            }*/

                        }else {
                            double tFirmBindNum = count * nFirmBindNum1 + lastMonthoPowerBindRate.getNoresideConsBind();
                            oIndexb.settFirmBindNum(tFirmBindNum);

                            /*if (1 == count){
                                double tFirmBindNum = nFirmBindNum1 + lastMonthoPowerBindRate.getNoresideConsBind();
                                qybdsNum += tFirmBindNum;
                                oIndexb.settFirmBindNum(tFirmBindNum);
                            }else {
                                double tFirmBindNum = nFirmBindNum1 + qybdsNum;
                                qybdsNum += tFirmBindNum;
                                oIndexb.settFirmBindNum(tFirmBindNum);
                            }*/
                        }
                    }
                }
                oIndexb.setYear(oIndexBreakdown.getYear());
                oIndexb.setPeriod(oIndexBreakdown.getPeriod());
                oIndexb.setUnit(orgNo);
                oIndexBreakdownListOver.add(oIndexb);
            }
        }
        return oIndexBreakdownListOver;
    }

    @Override
    public List<OIndexBreakdown> getCityBreakDownData(String year, String period) {
        // 获取当前登录人的deptId
        LoginUser loginUser = tokenService.getLoginUser();
        String unit = loginUser.getSysUser().getDeptId();
        List<OIndexBreakdown> oIndexBreakdownList = oIndexBreakdownMapper.getCityIndexDetailList(unit,year,period);
        return oIndexBreakdownList;
    }

    @Override
    public void countLastMonthIndexCompletion() throws IOException {
        // 统计出上个月的完成情况，判断是否有剩余指标未完成，如果有则记录，没有
        // 获取上个月的开始时间
        Date beginDayOfLastMonth = DateUtils.getBeginDayOfLastMonth();
        String startTime = DateUtils.longSdf.format(beginDayOfLastMonth);
        // 获取上个月的结束时间
        Date endDate = DateUtils.getEndDayOfLastMonth();
        String eTime = DateUtils.longSdf.format(endDate);
        String sTime = eTime.substring(0,10) + " 00:00:00";

        // 获取本月
        int nowMonth = DateUtils.getNowMonth();
        if (nowMonth > 1){
            // 查询所有的地市
            R r = remoteDeptService.getAllCityAndDistrictInfo();
            if (null != r && 200 == r.getCode()){
                List<SysDept> sysDeptList = getSysDepts(r);
                for (SysDept sysDept : sysDeptList){
                    OIndexBreakdown oIndexBreakdown = new OIndexBreakdown();
                    // 查询累计绑定数和累计注册数
                    OSgappStat oSgappStat = ioSgappStatService.getOsgappStatByUnit(sysDept.getDeptId(),sTime,eTime);
                    // 前一月
                    int lastMonth = 8 - 1;
                    OIndexBreakdown cityOIndexCurrent = oIndexBreakdownMapper.getIndexByUnitAndYearAndMonth(sysDept.getDeptId(), DateUtils.getNowYear().toString(), String.valueOf(lastMonth));
                    if (null != cityOIndexCurrent && null != oSgappStat){
                        // 上月完成的累计注册数
                        Long sgappTRegNum = oSgappStat.getSgappTRegNum();
                        // 上月的目标累计注册数
                        Double aDouble = cityOIndexCurrent.gettRegNum();
                        if (null != aDouble){
                            if (aDouble > sgappTRegNum){
                                double unDoneTRegNum = aDouble - sgappTRegNum;
                                oIndexBreakdown.settRegUndone((int) unDoneTRegNum);
                            }
                        }

                        // 上月完成的累计绑定数
                        Long sgappTBindNum = oSgappStat.getSgappTBindNum();
                        // 上月的目标累计绑定数
                        Double aDouble1 = cityOIndexCurrent.gettBindNum();
                        if (null != aDouble1) {
                            if (aDouble1 > sgappTBindNum) {
                                double unDoneTBindNum = aDouble1 - sgappTBindNum;
                                oIndexBreakdown.settBindUndone((int) unDoneTBindNum);
                            }
                        }
                        // 上月完成的累计企业用户绑定数
                        OPowerBindRate oPowerBindRateCity = ioPowerBindRateService.selectOPowerBindRateByUnit(sysDept.getDeptId(),sTime,eTime);
                        if (oPowerBindRateCity != null){
                            long tFirmBindNum = oSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind();
                            // 上月的目标累计企业绑定数
                            Double aDouble2 = cityOIndexCurrent.gettFirmBindNum();
                            if (null != aDouble2) {
                                if (aDouble2 > tFirmBindNum) {
                                    double nNum = aDouble2 - tFirmBindNum;
                                    oIndexBreakdown.settFirmUndone((int) nNum);
                                }
                            }
                        }
                        // 统计上月完成的新增注册数和绑定数
                        OSgappStat oSgappStat1 = ioSgappStatService.countNewNumByUnit(sysDept.getDeptId(), startTime, eTime);
                        //  未完成的新增注册数 =  上月目标新增注册数 - 上月完成的新增注册数
                        Double aDouble2 = cityOIndexCurrent.getnRegNum();
                        if (null != oSgappStat1){
                            Long sgappDRegNum = oSgappStat1.getSgappDRegNum();
                            if (null != aDouble2) {
                                if (sgappDRegNum < aDouble2) {
                                    double v = aDouble2 - sgappDRegNum;
                                    oIndexBreakdown.setnRegUndone((int) v);
                                }
                            }
                            // 未完成的新增绑定数 = 上月目标新增绑定数 - 上月完成的新增绑定数
                            Double aDouble3 = cityOIndexCurrent.getnBindNum();
                            Long sgappDBindNum = oSgappStat1.getSgappDBindNum();
                            if (null != aDouble3) {
                                if (sgappDBindNum < aDouble3) {
                                    double v = aDouble3 - sgappDBindNum;
                                    oIndexBreakdown.setnBindUndone((int) v);
                                }
                            }
                        }

                        // 查询上上月完成的高压绑定数和非居民绑定数
                        if (nowMonth == 2){
                            sTime = DateUtils.getLastYear() + "-12-31 00:00:00";
                            eTime = DateUtils.getLastYear() + "-12-31 23:59:59";
                        }else {
                            int month = nowMonth - 2;
                            Date endTime = DateUtils.getEndTime(DateUtils.getNowYear(), month);
                            eTime = DateUtils.longSdf.format(endTime);
                            sTime = eTime.substring(0,10) + " 00:00:00";
                        }
                        OSgappStat alloSgappStat = ioSgappStatService.getOsgappStatAndOpowerRateByUnit(sysDept.getDeptId(),sTime,eTime);
                        // 未完成的新增企业绑定数 = 本月目标新增企业绑定数 - 本月完成的新增企业绑定数
                        if (null != alloSgappStat){
                            // 本月完成的新增企业绑定数
                            if (null != alloSgappStat.getHigBindNum() && null != alloSgappStat.getFirmBindNum() && null != oPowerBindRateCity.getNoresideConsBind()){
                                long nfirmBindNum = (oSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind()) - (alloSgappStat.getHigBindNum() + alloSgappStat.getFirmBindNum());
                                // 本月目标新增企业绑定数
                                Double aDouble4 = cityOIndexCurrent.getnFirmBindNum();
                                if (null != aDouble4) {
                                    if (aDouble4 > nfirmBindNum) {
                                        double v = aDouble4 - nfirmBindNum;
                                        oIndexBreakdown.setnFirmUndone((int) v);
                                    }
                                }
                            }else if (null != alloSgappStat.getHigBindNum() && null == alloSgappStat.getFirmBindNum() && null == oPowerBindRateCity){
                                long nfirmBindNum = (oSgappStat.getHigBindNum()) - alloSgappStat.getHigBindNum();
                                // 本月目标新增企业绑定数
                                Double aDouble4 = cityOIndexCurrent.getnFirmBindNum();
                                if (null != aDouble4){
                                    if (aDouble4 > nfirmBindNum){
                                        double v = aDouble4 - nfirmBindNum;
                                        oIndexBreakdown.setnFirmUndone((int)v);
                                    }
                                }

                            }else {
                                long nfirmBindNum = (oSgappStat.getHigBindNum() + oPowerBindRateCity.getNoresideConsBind()) - alloSgappStat.getFirmBindNum();
                                // 本月目标新增企业绑定数
                                Double aDouble4 = cityOIndexCurrent.getnFirmBindNum();
                                if (null != aDouble4){
                                    if (aDouble4 > nfirmBindNum){
                                        double v = aDouble4 - nfirmBindNum;
                                        oIndexBreakdown.setnFirmUndone((int)v);
                                    }
                                }
                            }

                        }
                        OIndexBreakdown oIndexBreakdown1 = oIndexBreakdownMapper.selectOIndexBreakdownByYearAndUnit(sysDept.getDeptId(),DateUtils.getNowYear().toString(),lastMonth+"");
                        if (null != oIndexBreakdown1){
                            oIndexBreakdown.setId(oIndexBreakdown1.getId());
                            oIndexBreakdown.setUnit(sysDept.getDeptId());
                            oIndexBreakdown.setPeriod(lastMonth+"");
                            oIndexBreakdown.setYear(DateUtils.getNowYear().toString());
                            oIndexBreakdown.setUpdateTime(DateUtils.getNowDate());
                            oIndexBreakdownMapper.updateOIndexBreakdown(oIndexBreakdown);
                            logger.info("---------单位"+sysDept.getDeptId() + "，"+DateUtils.getNowYear() +"-"+lastMonth+"月的指标情况统计完成---------");
                        }
                    }

                }
            }
        }






    }

    @Override
    public String isDoBreakDown(String year) {
        // 获取当前登录人的deptId
        LoginUser loginUser = tokenService.getLoginUser();
        String unit = loginUser.getSysUser().getDeptId();
        List<OIndexBreakdown> oIndexBreakdowns = oIndexBreakdownMapper.selectListByYearAndUnit(unit, year);
        if (oIndexBreakdowns.size() > 0){
            return "2";
        }else {
            return "1";
        }
    }

    /*public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date beginTime = DateUtils.getBeginTime(Integer.valueOf("2021"), 1);
//        String starttime = sdf.format(beginTime);
//        String s = DateUtils.judgeStatTime("2021-06-02");
        String aa = "18563.0";
        double aDouble = Double.valueOf(aa);
        System.out.println(aDouble);
        int aDouble1 = (int) aDouble;
         System.out.println(aDouble1);
    }*/
}
